package tv_model;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import view.Emulator_tv;


public class Tv {
	
	private String id;
	private int numChannel;
	private String channel;
	private String prog;
	private String gender;
	private String startTime;
	private String endTime;
	private String description;
	private String webSite;
	
	private int numChannel_actual;
	private	JSONArray json;
	
	public Tv(String Id, int numChannel, String tntData){
		this.id = Id;
		this.numChannel_actual = 0;
		
		if(tntData != null){
			try {
				json = (JSONArray)new JSONParser().parse(tntData);
				JSONObject jsonChannel = (JSONObject)json.get(numChannel_actual);
	
				this.numChannel = Integer.parseInt((String)jsonChannel.get("numChannel"));
				this.channel = (String) jsonChannel.get("channel");
				this.prog = (String) jsonChannel.get("prog");
				this.gender = (String) jsonChannel.get("gender");
				this.startTime = (String) jsonChannel.get("startTime");
				this.endTime = (String) jsonChannel.get("endTime");
				this.webSite = (String) jsonChannel.get("webSite");
				this.description = (String) jsonChannel.get("description");			
				
			} catch (ParseException e) {
				e.printStackTrace();
			}     
		}
	}
	
	public String toJSON(){
		
		String jString = "{\"tv\":	" +
				"{\"id\":\""+id+"\"," +
				" \"numChannel\":\""+numChannel+"\"," +
				" \"channel\":\""+channel+"\"," +
				" \"prog\":\""+prog+"\"," +
				" \"gender\":\""+gender+"\"," +
				" \"startTime\":\""+startTime+"\"," +
				" \"endTime\":\""+endTime+"\"," +
				" \"webSite\":\""+webSite+"\"," +
				" \"description\":\""+description+"\"" +
				"}}";
		
		return jString;
	}
	
	public String toString(){
		return "Channel["+numChannel+"]: " + channel 
		+ "\nProgramation: " + prog 
		+ "\nGender: " + gender
		+ "\nSchedule: " + startTime + "-" + endTime
		+"\nURL: " + webSite
		+ "\nDescription: " + description;
	}
	
	public void changeChannel(Constants.Operation op){
		if(op == Constants.Operation.PLUS){
			if(numChannel_actual == 18){
				numChannel_actual = 0;
				setChannelNum(numChannel_actual);
			}else{
				numChannel_actual++;
				setChannelNum(numChannel_actual);
			}
		}else {
			if(numChannel_actual==0){
				numChannel_actual=18;
				setChannelNum(numChannel_actual);
			}else{
				numChannel_actual--;
				setChannelNum(numChannel_actual);
			}
		}
		Emulator_tv.notifyInfo();
	}
	
	/*Getters*/
	
	public String getId(){return this.id;}
	
	public String getChannel(){	return this.channel;}
	
	public String getProg(){return this.prog;}
	
	public String getGender(){return this.gender;}
	
	public String getStartTime(){return this.startTime;	}
	
	public String getEndTime(){	return this.endTime;}
	
	public String getWebSite(){return this.webSite;}

	public String getDescription(){	return this.description;}
		
	/*Setters*/
	
	public void setChannelNum(int numChannel){
		JSONObject jsonChannel =null;
		
		jsonChannel = (JSONObject)json.get(numChannel);

		if(jsonChannel != null){
			this.numChannel = Integer.parseInt((String)jsonChannel.get("numChannel"));
			this.channel = (String) jsonChannel.get("channel");
			this.prog = (String) jsonChannel.get("prog");
			this.gender = (String) jsonChannel.get("gender");
			this.startTime = (String) jsonChannel.get("startTime");
			this.endTime = (String) jsonChannel.get("endTime");
			this.webSite = (String) jsonChannel.get("webSite");
			this.description = (String) jsonChannel.get("description");
		}
		
	}
	
	public void setChannel(String channel){	this.channel = channel;	}
	
	public void setProg(String prog){ this.prog = prog;	}
	
	public void setGender(String gender){this.gender = gender;}
	
	public void setStartTime(String startTime){	this.startTime = startTime;	}
	
	public void setDndTime(String endTime){	this.endTime = endTime;	}

	public void setWebSite(String web){	this.webSite = web;	}

	public void setDescription(String des){	this.description = des;	}
	
	

}
